package com.xuanwu.shop.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;

import org.junit.Assert;
import org.junit.Test;

import com.xuanwu.shop.dao.CustomerDaoInterface;
import com.xuanwu.shop.entity.Customer;
import com.xuanwu.shop.util.StringUtil;

public class CustomerDaoImpl extends BaseDao implements CustomerDaoInterface {

	/**
	 * 保存顾客
	 * 
	 * @param customer
	 *            顾客对象
	 * @return 保存成功返回true，不成功放回false
	 */
	public boolean save(Customer customer) {
		boolean result = false;
		try {
			String sql = "insert into customer(id,username,passwd,email,truename,sex,phone,addr) values('"
					+ customer.getId() + "','" + customer.getUname() + "','" + customer.getPasswd() + "','"
					+ customer.getEmail() + "','" + customer.getRealName() + "'," + customer.getGender() + ",'"
					+ customer.getPhone() + "','')";
			sql = "insert into customer(id,username,passwd,email,truename,sex,phone,addr) values(?,?,?,?,?,?,?,?)";
			System.out.println(sql);
			Connection conn = getConnection();
			//Statement sta = conn.createStatement();
			PreparedStatement sta = conn.prepareStatement(sql);
			sta.setString(1, customer.getId());
			sta.setString(2, customer.getUname());
			sta.setString(3, customer.getPasswd());
			sta.setString(4, customer.getEmail());
			sta.setString(5, customer.getRealName());
			sta.setInt(6, customer.getGender());
			sta.setString(7, customer.getPhone());
			sta.setString(8, "");
			int num = sta.executeUpdate();
			if (num > 0) {
				result = true;
			}
			this.close(conn, sta, null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean updatePasswd(String id,String passwd){
		boolean result = false;
		Connection conn = null;
		PreparedStatement sta = null;
		String sql = "update customer set passwd=? where id=?";
		try {
			conn = getConnection();
			sta = conn.prepareStatement(sql);
			sta.setString(1, passwd);
			sta.setString(2, id);
			int num = sta.executeUpdate();
			if(num > 0){
				result = true;
			}
			this.close(conn, sta, null);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	@Test
	public void testSave(){
		Customer customer = new Customer();
		customer.setEmail("962333959@qq.com");
		customer.setGender(1);
		customer.setId(StringUtil.createId(new Date()));
		customer.setPasswd("zhang");
		customer.setPhone("15300000220");
		customer.setRealName("张先森");
		customer.setUname("zhang1314");
		Assert.assertTrue(save(customer));
	}
	
	@Test
	public void testUpdatePasswd(){
		String id = "20160319142645879391";
		String passwd = "nitemedezwq";
		Assert.assertTrue(updatePasswd(id, passwd));
	}

	@Override
	public Customer findByName(String uname) {
		Connection conn = null;
		PreparedStatement sta = null;
		ResultSet rs = null;
		Customer cus = null;
		String sql = "select id,passwd,email,truename,sex,phone from customer where username=?";
		try {
			conn = this.getConnection();
			sta = conn.prepareStatement(sql);
			sta.setString(1, uname);
			rs = sta.executeQuery();
			if(rs.next()){
				cus = new Customer();
				cus.setId(rs.getString(1));
				cus.setPasswd(rs.getString(2));
				cus.setEmail(rs.getString(3));
				cus.setRealName(rs.getString(4));
				cus.setGender(rs.getInt(5));
				cus.setPhone(rs.getString(6));
				cus.setUname(uname);
			}
			this.close(conn, sta, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cus;
	}

	@Override
	public Customer findById(String id) {
		Customer customer = null;
		String sql = "select username,passwd,email,truename,sex,phone from customer where id=?";
		try {
			this.conn = this.getConnection();
			this.psta = this.conn.prepareStatement(sql);
			this.psta.setString(1, id);
			this.rs = this.psta.executeQuery();
			if(rs.next()){
				customer = new Customer();
				customer.setUname(rs.getString(1));
				customer.setPasswd(rs.getString(2));
				customer.setEmail(rs.getString(3));
				customer.setRealName(rs.getString(4));
				customer.setGender(rs.getInt(5));
				customer.setPhone(rs.getString(6));
				customer.setId(id);
			}
			this.close(conn, psta, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return customer;
	}

	@Override
	public boolean deleteById(String id) {
		boolean result = false;
		String sql = "delete from customer where id=?";
		try {
			conn = getConnection();
			psta = conn.prepareStatement(sql);
			psta.setString(1, id);
			int num  = psta.executeUpdate();
			if(num > 0){
				result = true;
			}
			close(conn, psta, rs);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
}
